# -*- coding: utf-8 -*-
"""
Created on Mon May 19 00:39:58 2024

@author: 何敏
"""

import pandas as pd
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.tools.tools import add_constant
# 假设你的数据框名称是df
df = pd.read_csv("data.csv",encoding='GB2312')

# 设置面板数据格式：省份和年份为索引
df = df.set_index(["省份", "年份"])
# 假设 X 是你的解释变量数据框
X = df[["智能制造水平", "平均受教育年限", "固定资产投资总额", "第三产业增加值比GDP", "一般预算支出比GDP", "城镇化率"]]

# 为了计算 VIF，先添加常数项
X_with_const = add_constant(X)

# 计算 VIF
vif_data = pd.DataFrame()
vif_data["Variable"] = X_with_const.columns
vif_data["VIF"] = [variance_inflation_factor(X_with_const.values, i) for i in range(X_with_const.shape[1])]

# 输出 VIF 值
print(vif_data)